package cn.com.zal.cgweb.common.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;



import com.sun.jmx.snmp.Timestamp;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class LsdUtils {
	
	
	public static byte[] zipIt(byte[] buf) {

		String filename = String.valueOf(System.currentTimeMillis()) + ".xml";// new
																				// java.util.Date().toString();
		return zipIt(buf, filename);
	}

	public static byte[] zipIt(byte[] buf, String filename) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		ZipOutputStream zos = new ZipOutputStream(byteStream);
		// String filename = new java.util.Date().toString();
		try {
			zos.putNextEntry(new ZipEntry(filename));
			zos.write(buf);
			zos.flush();
			zos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return byteStream.toByteArray();
	}

	public static byte[] unzipIt(byte[] buf) {
		ByteArrayInputStream byteStream = new ByteArrayInputStream(buf);
		ZipInputStream zis = new ZipInputStream(byteStream);
		BufferedInputStream bis = new BufferedInputStream(zis);
		ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();

		try {
			ZipEntry ze;
			int x;
			while ((ze = zis.getNextEntry()) != null) {
				System.out.println(ze);

				while ((x = bis.read()) != -1) {
					byteOutStream.write(x);
				}
			}
			bis.close();
			byteOutStream.close();
		} catch (IOException e) {

			e.printStackTrace();
			return null;
		}

		return byteOutStream.toByteArray();
	}

	public static String getZipFileFilename(byte[] buf) {
		ByteArrayInputStream byteStream = new ByteArrayInputStream(buf);
		ZipInputStream zis = new ZipInputStream(byteStream);

		String filename = null;
		try {
			ZipEntry ze;

			int x;
			while ((ze = zis.getNextEntry()) != null) {
				System.out.println(ze);

				filename = ze.getName();
				break;
			}

		} catch (IOException e) {

			e.printStackTrace();
		}

		return filename;
	}

	public static byte[] gzipIt(byte[] buf) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

		try {
			GZIPOutputStream zos = new GZIPOutputStream(byteStream);
			zos.write(buf);
			zos.flush();
			zos.close();

			byteStream.close();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return byteStream.toByteArray();
	}

	public static byte[] ungzipIt(byte[] buf) {
		ByteArrayInputStream byteStream = new ByteArrayInputStream(buf);

		ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
		// ByteArrayInputStream byteIn = new ByteArrayInputStream();
		try {
			// BufferedReader in2 = new BufferedReader( new
			// InputStreamReader(new GZIPInputStream(byteStream)));
			GZIPInputStream zis = new GZIPInputStream(byteStream);
			final int ONCE_READ_COUNT = 5;
			byte[] cont = new byte[ONCE_READ_COUNT];
			int startPos = 0;
			int gotBytes;
			while (zis.available() > 0) {
				// byteOutStream.write(zis.read());
				gotBytes = zis.read(cont, 0, ONCE_READ_COUNT);
				if (gotBytes > 0) {
					byteOutStream.write(cont, 0, gotBytes);
					startPos += gotBytes;
					if (gotBytes != ONCE_READ_COUNT) {
						break;
					}
				} else {
					break;
				}
			}
			byteOutStream.close();
			zis.close();
			// byteOutStream.close();
			// String s;
			// String s1 = new String(byteOutStream.toByteArray());
			// System.out.println(s1);
			// while ((s = in2.readLine()) != null){
			//
			// System.out.println(s);
			// }

		} catch (IOException e) {

			e.printStackTrace();
		}

		return byteOutStream.toByteArray(); // byteOutStream.toByteArray();
	}

	public static String getRandomStr(int len) {
		StringBuilder sb = new StringBuilder();
		final String contents = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random rd = new Random();
		final int size = contents.length();
		for (int i = 0; i < len; ++i) {
			sb.append(contents.charAt(rd.nextInt(size)));

		}
		rd = null;
		return sb.toString();
	}

	public static MessageDigest getDigest(String algorithm) {
		try {
			return MessageDigest.getInstance(algorithm);
		} catch (NoSuchAlgorithmException e) {

			e.printStackTrace();
			return null;
		}
	}

	public static byte[] getMD5HashValue(byte[] content) {
		MessageDigest hash = getDigest("MD5");
		return hash.digest(content);
	}

	public static byte[] getPasswordMd5Hash(String pw) {
		MessageDigest hash = getDigest("MD5");
		return hash.digest(pw.getBytes());
	}
	

	public static String encodeToBase64(byte[] ori) {
		BASE64Encoder encoder = new BASE64Encoder();
		return (encoder.encode(ori));
	}

	public static String getFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

	public static byte[] decodeFromBASE64(String s) {
		if (s == null)
			return null;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s);
			return b;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Date o;
		System.out.println(new java.util.Date().toString());

	}

	public static java.util.Date getDateFromStr(String dateStr, String format) {
		// default foramt is yyyy-MM-dd
		if (format == null) {
			format = "yyyy-MM-dd";
		}
		java.util.Date parsedDate = null;
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		try {
			parsedDate = fmt.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return parsedDate;
	}

	public static String formatDateToStr(java.util.Date date, String fmt) {
		if (date == null) {
			return "";
		}
		if (fmt == null) {
			fmt = "yyyy-MM-dd";
		}
		SimpleDateFormat df = new SimpleDateFormat(fmt);
		return df.format(date);
	}

	
}
